package pdn.sci.cs.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import pdn.sci.cs.entity.ChildBank;
import pdn.sci.cs.entity.GenericList;
import pdn.sci.cs.service.ChildBankService;
import pdn.sci.cs.service.GenericListService;

@Scope(value = "prototype")
public class ChildBankAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Autowired private ChildBankService childBankService;
	@Autowired private GenericListService genericListService;

	private String childId;
	private ChildBank childBank;
	private List<ChildBank> list;
	private List<GenericList> bankList;

	public String list() {
		search();
		return SUCCESS;
	}

	public String searchForm() {
		searchPopulate();
		return SUCCESS;
	}

	public String frame() {
		return SUCCESS;
	}

	public String add() {
		addMode();
		addPopulate();
		return SUCCESS;
	}

	public String search() {
		if(childId != null) {
			list = childBankService.findByChildId(childId);
		} else {
			addActionError("Please give a criteria");
		}

		return SUCCESS;
	}

	public String edit() {
		editMode();
		return view();
	}

	public String save() {
		System.out.println("called");
		if(childBank != null) {
			System.out.println("validate");
			validateChild();
			if(hasErrors()) {
				return INPUT;
			} else {
				if(operationMode == OPERATION_MODE.ADD && childBank.getId().isEmpty()) {
					setAddSettings(childBank);
					childBank = childBankService.save(childBank);
				} else if (operationMode == OPERATION_MODE.EDIT && !childBank.getId().isEmpty() ) {
					setUpdateSettings(childBank);
					childBankService.update(childBank);
				} else {
					addActionError("Error");
					return INPUT;
				}

			}
		} else {
			addActionError("Invalid Access");
			return INPUT;
		}
		return SUCCESS;
	}

	public String view() {

		if(id == null || id.isEmpty()) {
			addActionError("Invalid Access");
			return INPUT;
		} else {
			childBank = childBankService.findById(id);
			childId = childBank.getChild().getId();
			if(childBank == null) {
				addActionError("Item that your are searching could not be found");
			}
		}
		return SUCCESS;
	}

	public String delete() {
		if(this.id.isEmpty()) {
			addActionError("Could not delete the entry, id is missing");
			return INPUT;
		} else {
			childBankService.delete(id);
			return list();
		}

	}

	private void searchPopulate() {

	}

	private void addPopulate() {

	}

	private void validateChild() {
		if(childBank.getBankName() == null) {
			addFieldError("childBank.bankName", "Bank name cannot be empty");
		}

		if(childBank.getAccountNumber().isEmpty()) {
			addFieldError("childBank.accountNumber", "Account number cannot be empty");
		}
	}

	public String getChildId() {
		return childId;
	}

	public void setChildId(String childId) {
		this.childId = childId;
	}

	public ChildBank getChildBank() {
		return childBank;
	}

	public void setChildBank(ChildBank childBank) {
		this.childBank = childBank;
	}

	public List<ChildBank> getList() {
		return list;
	}

	public void setList(List<ChildBank> list) {
		this.list = list;
	}

	public List<GenericList> getBankList() {
		bankList = genericListService.findListByCategoryId("C012");
		return bankList;
	}

	public void setBankList(List<GenericList> bankList) {
		this.bankList = bankList;
	}



}
